home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / mac / files / t_sys5 / 92052tar.gz / 920528.tar / nrs.c < prev    next >
C/C++ Source or Header  |  1992-05-14  |  5KB  |  230 lines

  1. /* @(#) $Header: nrs.c,v 1.7 92/05/14 13:20:22 deyke Exp $ */
  2.  
  3. /* This module implements the serial line framing method used by
  4.  * net/rom nodes.  This allows the net/rom software to talk to
  5.  * an actual net/rom over its serial interface, which is useful
  6.  * if we want to do packet switching for multi-line wormholes.
  7.  *
  8.  * Copyright 1989 Dan Frank, W9NK
  9.  */
  10. #include <stdio.h>
  11. #include "global.h"
  12. #include "timer.h"
  13. #include "mbuf.h"
  14. #include "iface.h"
  15. #include "pktdrvr.h"
  16. #include "ax25.h"
  17. #include "nrs.h"
  18. #include "asy.h"
  19. #include "trace.h"
  20. #include "commands.h"
  21.  
  22. static struct mbuf *nrs_encode __ARGS((struct mbuf *bp));
  23. static struct mbuf *nrs_decode __ARGS((int dev,int c));
  24.  
  25. /* control structures, sort of overlayed on async control blocks */
  26. struct nrs Nrs[ASY_MAX];
  27.  
  28. /* Send a raw net/rom serial frame */
  29. int
  30. nrs_raw(iface,bp)
  31. struct iface *iface;
  32. struct mbuf *bp;
  33. {
  34.     struct mbuf *bp1;
  35.  
  36.     dump(iface,IF_TRACE_OUT,CL_AX25,bp);
  37.     iface->rawsndcnt++;
  38.     iface->lastsent = secclock();
  39.  
  40.     if((bp1 = nrs_encode(bp)) == NULLBUF){
  41.         free_p(bp);
  42.         return -1;
  43.     }
  44.     return Nrs[iface->xdev].send(iface->dev,bp1);
  45. }
  46.  
  47. /* Encode a packet in net/rom serial format */
  48. static struct mbuf *
  49. nrs_encode(bp)
  50. struct mbuf *bp;
  51. {
  52.     struct mbuf *lbp;       /* Mbuf containing line-ready packet */
  53.     register char *cp;
  54.     int c;
  55.     unsigned char csum = 0;
  56.  
  57.     /* Allocate output mbuf that's twice as long as the packet.
  58.      * This is a worst-case guess (consider a packet full of STX's!)
  59.      * Add five bytes for STX, ETX, checksum, and two nulls.
  60.      */
  61.     lbp = alloc_mbuf((int16)(2*len_p(bp) + 5));
  62.     if(lbp == NULLBUF){
  63.         /* No space; drop */
  64.         free_p(bp);
  65.         return NULLBUF;
  66.     }
  67.     cp = lbp->data;
  68.  
  69.     *cp++ = STX;
  70.  
  71.     /* Copy input to output, escaping special characters */
  72.     while((c = PULLCHAR(&bp)) != -1){
  73.         switch(c){
  74.         case STX:
  75.         case ETX:
  76.         case DLE:
  77.             *cp++ = DLE;
  78.             /* notice drop through to default */
  79.         default:
  80.             *cp++ = c;
  81.         }
  82.         csum += c;
  83.     }
  84.     *cp++ = ETX;
  85.     *cp++ = csum;
  86.     *cp++ = NUL;
  87.     *cp++ = NUL;
  88.  
  89.     lbp->cnt = cp - lbp->data;
  90.     return lbp;
  91. }
  92. /* Process incoming bytes in net/rom serial format
  93.  * When a buffer is complete, return it; otherwise NULLBUF
  94.  */
  95. static struct mbuf *
  96. nrs_decode(dev,c)
  97. int dev;        /* net/rom unit number */
  98. char c;         /* Incoming character */
  99. {
  100.     struct mbuf *bp;
  101.     register struct nrs *sp;
  102.  
  103.     sp = &Nrs[dev];
  104.     switch(sp->state) {
  105.         case NRS_INTER:
  106.             if(uchar(c) == STX) {   /* look for start of frame */
  107.                 sp->state = NRS_INPACK; /* we're in a packet */
  108.                 sp->csum = 0;                           /* reset checksum */
  109.             }
  110.             return NULLBUF;
  111.         case NRS_CSUM:
  112.             bp = sp->rbp;
  113.             sp->rbp = NULLBUF;
  114.             sp->rcnt = 0;
  115.             sp->state = NRS_INTER;  /* go back to inter-packet state */
  116.             if(sp->csum == uchar(c)) {
  117.                 sp->packets++;
  118.             } else {
  119.                 free_p(bp);     /* drop packet with bad checksum */
  120.                 bp = NULLBUF;
  121.                 sp->errors++;   /* increment error count */
  122.             }
  123.             return bp;
  124.         case NRS_ESCAPE:
  125.             sp->state = NRS_INPACK; /* end of escape */
  126.             break;                  /* this will drop through to char processing */
  127.         case NRS_INPACK:
  128.             switch (uchar(c)) {
  129.             /* If we see an STX in a packet, assume that previous */
  130.             /* packet was trashed, and start a new packet */
  131.             case STX:
  132.                 free_p(sp->rbp);
  133.                 sp->rbp = NULLBUF;
  134.                 sp->rcnt = 0;
  135.                 sp->csum = 0;
  136.                 sp->errors++;
  137.                 return NULLBUF;
  138.             case ETX:
  139.                 sp->state = NRS_CSUM;   /* look for checksum */
  140.                 return NULLBUF;
  141.             case DLE:
  142.                 sp->state = NRS_ESCAPE;
  143.                 return NULLBUF;
  144.             }
  145.     }
  146.     /* If we get to here, it's with a character that's part of the packet.
  147.      * Make sure there's space for it.
  148.      */
  149.     if(sp->rbp == NULLBUF){
  150.         /* Allocate first mbuf for new packet */
  151.         if((sp->rbp1 = sp->rbp = alloc_mbuf(NRS_ALLOC)) == NULLBUF) {
  152.             sp->state = NRS_INTER;
  153.             return NULLBUF; /* No memory, drop */
  154.         }
  155.         sp->rcp = sp->rbp->data;
  156.     } else if(sp->rbp1->cnt == NRS_ALLOC){
  157.         /* Current mbuf is full; link in another */
  158.         if((sp->rbp1->next = alloc_mbuf(NRS_ALLOC)) == NULLBUF){
  159.             /* No memory, drop whole thing */
  160.             free_p(sp->rbp);
  161.             sp->rbp = NULLBUF;
  162.             sp->rcnt = 0;
  163.             sp->state = NRS_INTER;
  164.             return NULLBUF;
  165.         }
  166.         sp->rbp1 = sp->rbp1->next;
  167.         sp->rcp = sp->rbp1->data;
  168.     }
  169.     /* Store the character, increment fragment and total
  170.      * byte counts
  171.      */
  172.     *sp->rcp++ = c;
  173.     sp->rbp1->cnt++;
  174.     sp->rcnt++;
  175.     sp->csum += uchar(c);   /* add to checksum */
  176.     return NULLBUF;
  177. }
  178.  
  179. /* Process net/rom serial line I/O */
  180. void
  181. nrs_recv(iface)
  182. struct iface *iface;
  183. {
  184.  
  185.     char *cp,buf[4096];
  186.     int cnt,dev;
  187.     struct mbuf *bp,*nbp;
  188.     struct nrs *np;
  189.     struct phdr phdr;
  190.  
  191.     dev = iface->xdev;
  192.     np = Nrs+dev;
  193.  
  194.     cnt = (*np->get)(iface->dev,cp=buf,sizeof(buf));
  195.     while(--cnt >= 0){
  196.         if((bp = nrs_decode(dev,*cp++)) == NULLBUF)
  197.             continue;
  198.         if((nbp = pushdown(bp,sizeof(phdr))) == NULLBUF){
  199.             free_p(bp);
  200.             continue;
  201.         }
  202.         phdr.iface = Nrs[dev].iface;
  203.         phdr.type = CL_AX25;
  204.         memcpy(&nbp->data[0],(char *)&phdr,sizeof(phdr));
  205.         enqueue(&Hopper,nbp);
  206.     }
  207.  
  208. }
  209. /* donrstat:  display status of active net/rom serial interfaces */
  210. int
  211. donrstat(argc,argv,p)
  212. int argc;
  213. char *argv[];
  214. void *p;
  215. {
  216.     register struct nrs *np;
  217.     register int i;
  218.  
  219.     printf("Interface   RcvB  NumReceived  CSumErrors\n");
  220.  
  221.     for(i = 0, np = Nrs; i < ASY_MAX; i++, np++)
  222.         if(np->iface != NULLIF)
  223.             if(printf(" %8s   %4d   %10lu  %10lu\n",
  224.              np->iface->name, np->rcnt,
  225.              np->packets, np->errors) == EOF)
  226.                 break;
  227.  
  228.     return 0;
  229. }
  230.